package klass.parse.domain;

import klass.parse.domain.attribute.Attribute;

import java.util.ArrayList;
import java.util.List;

public class FieldInfo {

    private static final short ACC_PUBLIC = 0x0001;
    private static final short ACC_PRIVATE = 0x0002;
    private static final short ACC_PROTECTED = 0x0004;
    private static final short ACC_STATIC = 0x0008;
    private static final short ACC_FINAL = 0x0010;
    private static final short ACC_VOLATILE = 0x0040;
    private static final short ACC_TRANSIENT = 0x0080;
    private static final short ACC_SYNTHETIC = 0x1000;
    private static final short ACC_ENUM = 0x4000;

    private String name;
    private String descriptor;
    private short flags;
    private List<Attribute> attributes;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescriptor() {
        return descriptor;
    }

    public void setDescriptor(String descriptor) {
        this.descriptor = descriptor;
    }

    public short getFlags() {
        return flags;
    }

    public void setFlags(short flags) {
        this.flags = flags;
    }

    public List<Attribute> getAttributes() {
        return attributes;
    }

    public void setAttributes(List<Attribute> attributes) {
        this.attributes = attributes;
    }

    public void addAttribute(Attribute attribute) {
        this.attributes.add(attribute);
    }

    public boolean isPublic() {
        return is(ACC_PUBLIC);
    }

    public boolean isPrivate() {
        return is(ACC_PRIVATE);
    }

    public boolean isProtected() {
        return is(ACC_PROTECTED);
    }

    public boolean isStatic() {
        return is(ACC_STATIC);
    }

    public boolean isFinal() {
        return is(ACC_FINAL);
    }

    public boolean isVolatile() {
        return is(ACC_VOLATILE);
    }

    public boolean isTransient() {
        return is(ACC_TRANSIENT);
    }

    public boolean isEnum() {
        return is(ACC_ENUM);
    }

    private boolean is(short check) {
        return check == (check & flags);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        if (isPublic()) builder.append(" public");
        if (isPrivate()) builder.append(" private");
        if (isProtected()) builder.append(" protected");
        if (isStatic()) builder.append(" static");
        if (isFinal()) builder.append(" final");
        if (isVolatile()) builder.append(" volatile");
        if (isTransient()) builder.append(" transient");
        builder.append(" ").append(descriptor);
        builder.append(" ").append(name);
        return builder.toString();
    }
}
